package com.nwcl.ysx.controller;

import com.nwcl.basic.bo.GoodsBO;
import com.nwcl.basic.bo.ResourcesBO;
import com.nwcl.basic.service.GoodsService;
import com.nwcl.basic.service.ResourcesService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Page;
import com.nwcl.common.result.Result;
import com.nwcl.common.utils.BeanUtil;
import com.nwcl.ysx.request.GoodsPageRequest;
import com.nwcl.ysx.vo.ResourceVO;
import com.nwcl.ysx.vo.GoodsDetailsVO;
import com.nwcl.ysx.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author liguiqin
 * @date 2023/5/24
 */
@RestController
@RequestMapping("/api/ysx/goods")
public class ApiGoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ResourcesService resourcesService;

    /*
     * @Description:分页查询商品列表
     * @Author liguiqin
     * @Date 2023/5/24 14:26
     * @param: param
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/24 14:26
     **/
    @GetMapping("/list")
    public Result list(GoodsPageRequest param) {
        Page<GoodsVO> resultPage = new Page<>();
        param.setStatus(Constant.GoodsStatus.up);//查询上架商品
        Page<GoodsBO> page = goodsService.page(param);
        List<GoodsVO> voList = warpList(page.getList());
        resultPage = new Page<>(page.getPageNum(), page.getPageSize(), page.getTotal(), voList);
        return Result.success(resultPage);
    }

    /*
     * @Description:  商品详情
     * @Author liguiqin
     * @Date 2023/5/29 14:50
     * @param: id
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/29 14:50
     **/
    @GetMapping("/details")
    public Result details(Integer id) {
        if (id == null) {
            return Result.fail("商品id不能为空");
        }
        GoodsBO goodsBO = goodsService.getById(id);
        GoodsDetailsVO detailsVO = BeanUtil.copy(goodsBO, GoodsDetailsVO.class);
        //设置商品图片资源
        List<ResourcesBO> resourcesBOS= resourcesService.listByPidTypeAndPids(Constant.ResourcesPidType.goods, Arrays.asList(id));
        List<ResourceVO> resourceVOS=BeanUtil.copyList(resourcesBOS, ResourceVO.class);
        detailsVO.setResourceList(resourceVOS);
        return Result.success(detailsVO);
    }

    /*
     * @Description: 封装商品返回数据信息
     * @Author liguiqin
     * @Date 2023/5/29 13:50
     * @param: list
     * @return java.util.List<com.nwcl.basic.vo.OrdersVO>
     * @UpdateDate 2023/5/29 13:50
     **/
    private List<GoodsVO> warpList(List<GoodsBO> list) {
        List<GoodsVO> voList = BeanUtil.copyList(list, GoodsVO.class);
        return voList;
    }

}
